package fangzhiyi_day0325.ArrayList;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author fangzhiyi
 * @version 1.0
 * @date 2025/3/25 10:41
 * @description
 */
public  class Dao {
   public static ArrayList<Student> stu = new ArrayList<>();
    public static Scanner INPUT= new Scanner(System.in);
    static{
        stu.add(new Student(001,"方知意",18,82));
        stu.add(new Student(002,"南风",20,87));
        stu.add(new Student(003,"高阳",22,92));
        stu.add(new Student(004,"高欣欣",19,85));
        stu.add(new Student(005,"万思思",17,89));
    }

    public void findStu(){
        for (int i = 0; i < stu.size(); i++) {
            System.out.println(stu.get(i));
        }
    }

    public void addStu(){
        System.out.println("请输入添加的学生的编号");
        int id = INPUT.nextInt();
        System.out.println("请输入添加的学生的姓名");
        String name = INPUT.next();
        System.out.println("请输入添加的学生的年龄");
        int age = INPUT.nextInt();
        System.out.println("请输入添加的学生的成绩");
        double score = INPUT.nextDouble();
        stu.add(new Student(id,name,age,score));
    }

    public void setStu(){
        System.out.println("请输入要修改的学生的id");
        int id = INPUT.nextInt();
        for (int i = 0; i < stu.size(); i++) {
            if (id==stu.get(i).getId()){
                System.out.println("请输入需要修改的成绩");
                double score = INPUT.nextDouble();
                stu.get(i).setScore(score);
                System.out.println("修改后的信息为:"+stu.get(i));
            }
        }
    }

    public void delStu(){
        System.out.println("请输入需要删除的学生的id");
        int id = INPUT.nextInt();
        for (int i = 0; i < stu.size(); i++) {
            if (id==stu.get(i).getId()){
                System.out.println(stu.get(i));
                System.out.println("请确认是否删除(y/n)");
                String num = INPUT.next();
                if (num.equals("y")){
                    stu.remove(i);
                }else {
                    return;
                }
            }
        }
    }

        public void ageSort(){
            for (int i = 0; i < stu.size(); i++) {
                for (int j = i+1; j < stu.size(); j++) {
                    if (stu.get(i).getAge()>stu.get(j).getAge()){
                       Student temp = stu.get(j);
                       stu.set(j,stu.get(i));
                       stu.set(i,temp);
                    }
                }
                System.out.println("按年龄从小到大排序为:"+stu.get(i));
            }
        }

    public void scoreSort(){
        for (int i = 0; i < stu.size(); i++) {
            for (int j = i+1; j < stu.size(); j++) {
                if (stu.get(i).getScore()<stu.get(j).getScore()){
                    Student temp = stu.get(j);
                    stu.set(j,stu.get(i));
                    stu.set(i,temp);
                }
            }
            System.out.println("按成绩从大到小排序为:"+stu.get(i));
        }
    }
}
